﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Notes.Model;
using Notes.Helpers;

namespace Notes
{
    public partial class DetailPage : PhoneApplicationPage
    {
        private string selectedIndex;

        public DetailPage()
        {
            InitializeComponent();

            #region application bar
            ApplicationBar.BackgroundColor = (Color)Resources["PhoneBackgroundColor"];
			ApplicationBarIconButton btnEdit = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
			btnEdit.Text = AppResources.strBarEdit;
			ApplicationBarIconButton btnDel = (ApplicationBarIconButton)ApplicationBar.Buttons[1];
			btnDel.Text = AppResources.strBarDel;
            #endregion

            DataContext = App.EditViewModel;
            Loaded += new RoutedEventHandler(DetailPage_Loaded);
		}

        private void DetailPage_Loaded(Object sender, RoutedEventArgs e)
        {
            if (NavigationContext.QueryString.TryGetValue("selectedIndex", out selectedIndex))
            {
                Deployment.Current.Dispatcher.BeginInvoke(delegate
                {
                    int _NoteId = int.Parse(selectedIndex);
                    App.EditViewModel.LoadNote(_NoteId);
                });
            }
            else
            {
                Messenger.ShowMessage(AppResources.strMessageTitleError, AppResources.strMessageTextNoteNotFound);
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                NavigationService.RemoveBackEntry();
            }
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (App.EditViewModel.Photos.Count > 0)
                ImagesListBox.Visibility = Visibility.Visible;
        }

        private void ApplicationBarEdit_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/EditPage.xaml?selectedIndex=" + selectedIndex, UriKind.Relative));
            //NavigationService.RemoveBackEntry();
        }

        private void ApplicationBarDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(AppResources.strMessageTextConfirmDelete, AppResources.strMessageTitleWarning, MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
				App.EditViewModel.Note.Edited = DateTime.Now;
                App.EditViewModel.Note.Deleted = true;
				App.db.SubmitChanges();
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
				NavigationService.RemoveBackEntry();
            }
        }

        private void ImagesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ImagesListBox.SelectedIndex == -1) return;
            NavigationService.Navigate(new Uri("/PicturePage.xaml?selectedItem=" + ImagesListBox.SelectedIndex, UriKind.Relative));
            ImagesListBox.SelectedIndex = -1;
        }

        private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            if ((e.Orientation & PageOrientation.Portrait) == (PageOrientation.Portrait))
            {
                LayoutRoot.Margin = new Thickness(0, 32, 0, 72);
            }
            else
            {
                LayoutRoot.Margin = new Thickness(72, 0, 72, 0);
            }
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            base.OnBackKeyPress(e);

            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

    }
}